package com.jf.demo;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.sys.android.util.DialogFactory;
import com.sys.android.util.FinalUntil;

import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Paint;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

@ContentView(R.layout.activity_main)
public class MainActivity extends Activity {
	@ViewInject(R.id.textView1)
	public TextView textView;

	@ViewInject(R.id.username)
	public EditText username;

	@ViewInject(R.id.password)
	public EditText password;

	private SharedPreferences useShare;

	private Editor editor;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ViewUtils.inject(this);// xml视图加载的处理
		textView.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
		textView.setClickable(true);
		// 用于用户名与密码的保存
		useShare = getSharedPreferences("userInfo", MODE_PRIVATE);
		editor = useShare.edit();
		if (!useShare.getString("username", "").equals("")) {
			Intent intent = new Intent(MainActivity.this, Mainmenu.class);
			startActivity(intent);
			this.finish();
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
//		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	public void createUser(View view) {
		// Toast.makeText(view.getContext(), "启动新窗体", Toast.LENGTH_LONG).show();
		Intent intent = new Intent(MainActivity.this, RegisterUser.class);
		startActivity(intent);
		this.finish();
	}

	@OnClick(R.id.button)
	public void login(View view) {
		
		final Dialog dialog = DialogFactory.creatRequestDialog(MainActivity.this,
				"登录中……");
		dialog.show();

		RequestParams params = new RequestParams();

		params.addQueryStringParameter("userID", username.getText().toString());
		params.addQueryStringParameter("password", password.getText()
				.toString());

		HttpUtils http = new HttpUtils();
		http.send(HttpMethod.POST, FinalUntil.ServerAddress+"loginPhoneUser",
				params, new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO Auto-generated method stub
						Log.i("RegisterUserNext", arg1);
					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						// arg0.result返回服务器结果
						Log.i("RegisterUserNext", arg0.result);
						if (arg0.result.equals("true")) {
							editor.putString("username", username.getText()
									.toString());
							editor.putString("password", password.getText()
									.toString());
							editor.commit();
							Intent intent = new Intent(MainActivity.this,
									Mainmenu.class);
							startActivity(intent);
							MainActivity.this.finish();
						} else {
							Toast.makeText(MainActivity.this, "用户名或密码错误", 0)
									.show();
							dialog.hide();
						}
					}
				});

	}
}
